package com.scut.statistics_website.service;

import com.scut.statistics_website.config.GlobalVariables;
import com.scut.statistics_website.model.Demand;
import com.scut.statistics_website.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@Component
public class DemandService {
    @Autowired
    private RedisUtil redisUtil;

    public ArrayList<String> getCachedDemands() {
        ArrayList<String> demandIds = new ArrayList<>();
        Set<Object> demandsSet = redisUtil.sGet(GlobalVariables.ALIVE_DEMAND_SET_KEY);
        for (Object demandId : demandsSet) {
            demandIds.add((String)demandId);
        }
        return demandIds;
    }

    public Demand getDemand(String id) {
        Map<Object, Object> rawDemand = redisUtil.hmget(id);
        return new Demand(id, (float)rawDemand.get("lon"), (float)rawDemand.get("lat"));
    }

}
